package com.black.play.component.box;

import com.black.play.common.FormComponent;
import com.black.play.common.FormDirection;
import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ylx
 */
@Getter
@Setter
public class AppCheckBox extends FormComponent<JPanel>
{
    private final String[] options;

    private boolean showSelectedAll = true;

    private final Map<String, JCheckBox> checkBoxMap = new HashMap<>();

    public AppCheckBox(String label, String[] options)
    {
        this(label, options, "");
    }

    public AppCheckBox(String label, String[] options, String value)
    {
        super(label, FormDirection.VERTICAL);
        this.options = options;
        this.value = value;
        this.constraints.gridx = 1;
        this.constraints.gridy = 0;
        this.constraints.gridwidth = GridBagConstraints.REMAINDER;
        // 初始化复选框
        if (showSelectedAll)
        {
            JCheckBox allCheckBox = new JCheckBox("全选");
            allCheckBox.addActionListener(e -> {
                boolean selected = allCheckBox.isSelected();
                for (JCheckBox checkBox : checkBoxMap.values())
                {
                    checkBox.setSelected(selected);
                }
            });
        }
        for (String option : options)
        {
            JCheckBox checkBox = new JCheckBox(option);
            if (isSelected(option))
            {
                checkBox.setSelected(true);
            }
            component.add(checkBox);
            checkBoxMap.put(option, checkBox);
        }
    }

    @Override
    public void initComponent()
    {
        this.component = new JPanel();
        this.component.setLayout(new BoxLayout(component, BoxLayout.Y_AXIS));
    }

    @Override
    public String getValue()
    {
        // 返回所有选中的项，以逗号分隔
        Set<String> selectedOptions = getValues();
        return String.join(",", selectedOptions);
    }

    @Override
    public void setText()
    {
        // 设置已选中的复选框
        Set<String> selectedOptions = parseSelectedValues(value);
        checkBoxMap.forEach((option, checkBox) -> checkBox.setSelected(selectedOptions.contains(option)));
    }

    private boolean isSelected(String option)
    {
        Set<String> selectedOptions = parseSelectedValues(value);
        return selectedOptions.contains(option);
    }

    private Set<String> parseSelectedValues(String value)
    {
        if (value == null || value.isEmpty())
        {
            return new HashSet<>();
        }
        return Arrays.stream(value.split(",")).collect(Collectors.toSet());
    }

    public Set<String> getValues()
    {
        return checkBoxMap.entrySet().stream()
            .filter(entry -> entry.getValue().isSelected())
            .map(Map.Entry::getKey)
            .collect(Collectors.toSet());
    }

    @Override
    public void addKeyListener(KeyListener l)
    {
        super.addKeyListener(l);
        checkBoxMap.values().forEach(checkBox -> checkBox.addKeyListener(l));
    }
}
